﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Canvas的角度，0在正南方
/// </summary>
public class AutoNumbers : MonoBehaviour
{
    public GaugeInfo gauge;
    public float len = 120F;
    public GameObject NumberTextPrefab;

    private void Start()
    {
        GeneratNumbers();
    }

    private IList<float> angleList = new List<float>();
    private void GeneratNumbers()
    {
        float splitAngle = gauge.splitAngleBold;

        int count = (int)( (gauge.maxAngle - gauge.minAngle) / splitAngle ) + 1;

        float angle = 0f;
        for (int i = 0; i < count; i++)
        {
            angle = 180 - (gauge.minAngle + i * splitAngle);
            if (angle < 0)
            {
                angle = 360 + angle;
            }
            angleList.Add(angle);
        }
        //===
        GameObject newInstance;
        Text number;
        float value;
        float a, b;
        float x, y;
        float radAngle = 0f;

        for (int i = 0; i < count; i++)
        {
            angle = angleList[i];

            newInstance = Instantiate<GameObject>(NumberTextPrefab);
            newInstance.transform.SetParent(transform);
            number = newInstance.GetComponent<Text>();

            radAngle = angle * Mathf.Deg2Rad;
            b = Mathf.Cos(radAngle) * len;
            a = Mathf.Sin(radAngle) * len;

            x = b;
            y = a;

            number.rectTransform.localScale = new Vector3(1, 1, 1);
            number.rectTransform.localPosition = new Vector3(x, y, 0);

            value = i * ( (gauge.max - gauge.min) / (count - 1) );
            if (gauge.invert)
            {
                value = (count - 1 - i) * ((gauge.max - gauge.min) / (count - 1));
            }
            number.text = string.Format("{0}", value);
        }
    }

}